package com.kyomonn.talkischeap.CodingResources;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.kyomonn.talkischeap.WebViewActivity;

public class TutorialTabButtonAdapter extends BaseAdapter {
    private final Context context;
    private final int resource;
    private final String[] texts;
    private final String[] urls;

    public TutorialTabButtonAdapter(Context context, int resource, String[] texts, String[] urls) {
        this.context = context;
        this.resource = resource;
        this.texts = texts;
        this.urls = urls;
    }

    @Override
    public int getCount() {
        return texts.length;
    }

    @Override
    public Object getItem(int i) {
        return texts[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;

        if (view == null) {
            view = LayoutInflater.from(context).inflate(resource, viewGroup, false);
            Button btn = (Button) view;
            holder = new ViewHolder(btn);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.bind(texts[i], new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), WebViewActivity.class);
                intent.putExtra("url", urls[i]);
                view.getContext().startActivity(intent);
            }
        });

        return view;
    }

    static class ViewHolder {

        Button button;

        public ViewHolder(Button view) {
            this.button = view;
        }

        public void bind(String text, View.OnClickListener callback) {
            button.setText(text);
            button.setOnClickListener(callback);
        }
    }
}
